package io.renren.modules.hos.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import io.renren.modules.hos.entity.DoctorEntity;
import io.renren.modules.hos.entity.PatientEntity;
import io.renren.modules.hos.form.AdviceForm;
import io.renren.modules.hos.service.DoctorService;
import io.renren.modules.hos.service.PatientService;
import io.renren.modules.hos.vo.DoctorMedicalAdviceVo;
import io.renren.modules.hos.vo.PatientMedicalAdviceVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.Query;

import io.renren.modules.hos.dao.MedicalAdviceDao;
import io.renren.modules.hos.entity.MedicalAdviceEntity;
import io.renren.modules.hos.service.MedicalAdviceService;


@Service("medicalAdviceService")
public class MedicalAdviceServiceImpl extends ServiceImpl<MedicalAdviceDao, MedicalAdviceEntity> implements MedicalAdviceService {
    @Autowired
    private DoctorService doctorService;
    @Autowired
    private PatientService patientService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<MedicalAdviceEntity> page = this.page(
                new Query<MedicalAdviceEntity>().getPage(params),
                new QueryWrapper<MedicalAdviceEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public boolean addAdvice(Long doctorId, AdviceForm adviceForm) {
        MedicalAdviceEntity medicalAdviceEntity = new MedicalAdviceEntity();
        medicalAdviceEntity.setAdviceContent(adviceForm.getContent());
        medicalAdviceEntity.setDoctorId(doctorId);
        medicalAdviceEntity.setAdviceContent(adviceForm.getContent());
        medicalAdviceEntity.setCreatTime(new Date());
        medicalAdviceEntity.setPatientId(adviceForm.getPatientId());
        return this.save(medicalAdviceEntity);
    }

    @Override
    public PageUtils queryByPatientId(Long patientId, Map<String, Object> params) {
        LambdaQueryWrapper<MedicalAdviceEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(MedicalAdviceEntity::getPatientId, patientId);
        List<MedicalAdviceEntity> medicalAdviceEntities = baseMapper.selectList(queryWrapper);
        List<PatientMedicalAdviceVo> patientMedicalAdviceVos = patientCopyList(medicalAdviceEntities);
        int pageSize = (int) params.get("pageSize");
        int pageIndex = (int) params.get("pageIndex");

        return new PageUtils(patientMedicalAdviceVos, patientMedicalAdviceVos.size(), pageSize, pageIndex);
    }

    @Override
    public PageUtils queryBuDoctorId(Long doctorId, Map<String, Object> params) {
        LambdaQueryWrapper<MedicalAdviceEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(MedicalAdviceEntity::getDoctorId, doctorId);
        List<MedicalAdviceEntity> medicalAdviceEntities = baseMapper.selectList(queryWrapper);
        List<DoctorMedicalAdviceVo> doctorMedicalAdviceVos = doctorCopyList(medicalAdviceEntities);
        int pageSize = (int) params.get("pageSize");
        int pageIndex = (int) params.get("pageIndex");

        return new PageUtils(doctorMedicalAdviceVos, doctorMedicalAdviceVos.size(), pageSize, pageIndex);
    }

    public List<PatientMedicalAdviceVo> patientCopyList(List<MedicalAdviceEntity> medicalAdviceEntities){
        List<PatientMedicalAdviceVo> patientMedicalAdviceVos = new ArrayList<>();
        for (MedicalAdviceEntity medicalAdviceEntity : medicalAdviceEntities) {
                patientMedicalAdviceVos.add(patientCopy(medicalAdviceEntity));
        }

        return patientMedicalAdviceVos;
    }

    public PatientMedicalAdviceVo patientCopy(MedicalAdviceEntity medicalAdviceEntity){
        DoctorEntity doctorEntity = doctorService.getById(medicalAdviceEntity.getDoctorId());
        PatientMedicalAdviceVo patientMedicalAdviceVo = new PatientMedicalAdviceVo();
        patientMedicalAdviceVo.setDoctorId(medicalAdviceEntity.getDoctorId());
        patientMedicalAdviceVo.setDoctorName(doctorEntity.getDoctorName());
        patientMedicalAdviceVo.setDoctorImg(doctorEntity.getDoctorImg());
        patientMedicalAdviceVo.setAdviceContent(medicalAdviceEntity.getAdviceContent());
        patientMedicalAdviceVo.setCreatTime(medicalAdviceEntity.getCreatTime());

        return patientMedicalAdviceVo;
    }

    public DoctorMedicalAdviceVo doctorCopy(MedicalAdviceEntity medicalAdviceEntity){
        PatientEntity patientEntity = patientService.getById(medicalAdviceEntity.getPatientId());
        DoctorMedicalAdviceVo doctorMedicalAdviceVo = new DoctorMedicalAdviceVo();
        doctorMedicalAdviceVo.setId(medicalAdviceEntity.getPatientId());
        doctorMedicalAdviceVo.setPatientName(patientEntity.getPatientName());
        doctorMedicalAdviceVo.setAdviceContent(medicalAdviceEntity.getAdviceContent());
        doctorMedicalAdviceVo.setCreatTime(medicalAdviceEntity.getCreatTime());

        return doctorMedicalAdviceVo;
    }

    public List<DoctorMedicalAdviceVo> doctorCopyList(List<MedicalAdviceEntity> medicalAdviceEntities){
        List<DoctorMedicalAdviceVo> doctorMedicalAdviceVos = new ArrayList<>();
        for (MedicalAdviceEntity medicalAdviceEntity : medicalAdviceEntities) {
            doctorMedicalAdviceVos.add(doctorCopy(medicalAdviceEntity));
        }

        return doctorMedicalAdviceVos;
    }

}